package com.cyedu.tsapp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyedu.tsapp.entity.AppInfo;
import com.cyedu.tsapp.entity.AppRecommended;
import com.cyedu.tsapp.entity.Result;
import com.cyedu.tsapp.service.IAppInfoService;
import com.cyedu.tsapp.service.IAppRecommendedService;
import com.cyedu.tsapp.utils.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Garne
 * @since 2021-06-26
 */
@RestController
@RequestMapping("/appRecommended")
public class AppRecommendedController {

    @Autowired
    IAppRecommendedService recommendedService;

    @Autowired
    IAppInfoService appInfoService;

    @ApiOperation(value = "获取切换场景的数据", notes = "")
    @RequestMapping(value = "/getChangeApps", method = RequestMethod.GET)
    public Result getChangeApps() {
        List<AppRecommended> ars = recommendedService.list();
        if (null != ars) {
            List<AppRecommended> resList = new ArrayList<>();
            String curGroup = "";
            AppRecommended recommended = null;
            List<AppInfo> infoList = null;
            for (int i = 0; i < ars.size(); i++) {
                AppRecommended ar = ars.get(i);
                if (StringUtils.isBlank(curGroup)) {
                    curGroup = ar.getGroup();
                }

                if (curGroup.equals(ar.getGroup())) {
                    if (recommended == null) {
                        recommended = new AppRecommended();
                        recommended.setGroup(ar.getGroup());
                        recommended.setId(ar.getId());
                        infoList = new ArrayList<>();
                        recommended.setAppInfos(infoList);
                    }
                    QueryWrapper queryWrapper = new QueryWrapper();
                    queryWrapper.eq("app_name", ar.getAppName());
                    AppInfo ai = appInfoService.getOne(queryWrapper);
                    if (null != ai) {
                        recommended.getAppInfos().add(ai);
                    }
                    if (i == ars.size() - 1) {
                        resList.add(recommended);
                        recommended = null;
                        infoList = null;
                    }
                } else {
                    resList.add(recommended);
                    recommended = null;
                    infoList = null;
                    curGroup = ar.getGroup();
                    if (recommended == null) {
                        recommended = new AppRecommended();
                        recommended.setGroup(ar.getGroup());
                        recommended.setId(ar.getId());
                        infoList = new ArrayList<>();
                        recommended.setAppInfos(infoList);
                    }
                    QueryWrapper queryWrapper = new QueryWrapper();
                    queryWrapper.eq("app_name", ar.getAppName());
                    AppInfo ai = appInfoService.getOne(queryWrapper);
                    if (null != ai) {
                        recommended.getAppInfos().add(ai);
                    }

                }
            }
            return ResultUtil.success(resList);
        }
        return ResultUtil.fail("没有数据");
    }
}
